# _*_ coding: utf-8 _*_
from odoo import models, fields, api


class StockPicking(models.Model):
    _inherit = "stock.picking"

    @api.multi
    def do_transfer(self):
        res = super(StockPicking, self).do_transfer()
        for picking in self:
            sale_id = picking.mapped("group_id.procurement_ids.sale_line_id.order_id")
            if sale_id.auto_generated and sale_id.auto_purchase_order_id:
                for purchase_picking in sale_id.sudo().auto_purchase_order_id.picking_ids.filtered(
                        lambda p: p.state not in ("cancel", "done")):
                    wizard = self.env["stock.immediate.transfer"].sudo().create({
                        "pick_id": purchase_picking.id
                    })
                    wizard.process()
        return res
